package com.tsingteng.cosfun.ui.cosfun.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.ImgBean;
import com.tsingteng.cosfun.utils.AppUtils;

import java.util.List;

public class TipOffsImgAdapter extends BaseAdapter {
    Context mContext = null;
    ViewHolder viewHolder = null;
    List<ImgBean> list = null;
    Resources rs = null;
    ImgOnClickListener imgOnClickListener = null;
    int rel_w = 0;
    int rel_h = 0;
    int img_w = 0;
    int img_h = 0;

    public TipOffsImgAdapter(Context mContext, Activity activity, List<ImgBean> list, ImgOnClickListener imgOnClickListener) {
        this.mContext = mContext;
        this.list = list;
        this.imgOnClickListener = imgOnClickListener;
        rs = mContext.getResources();
        rel_w = (int) (AppUtils.getScreenWidthAndHeight(activity)[0] - rs.getDimension(R.dimen.dp_15) * 5) / 4;
        rel_h = rel_w;

        img_w = (int) (rel_w - rs.getDimension(R.dimen.dp_10));
        img_h = img_w;

        Log.e("test", "rel_w=" + rel_w);
        Log.e("test", "rel_h=" + rel_h);
        Log.e("test", "img_w=" + img_w);
        Log.e("test", "img_h=" + img_h);
    }

    @Override
    public int getCount() {
        if (list == null)
            return 0;
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.tip_offs_img_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.rel = convertView.findViewById(R.id.tip_offs_img_item_rel);
            viewHolder.close_img = convertView.findViewById(R.id.del);
            viewHolder.count_tv = convertView.findViewById(R.id.tv);
            viewHolder.lincarema = convertView.findViewById(R.id.lincarema);
            viewHolder.img_img = convertView.findViewById(R.id.img);
            viewHolder.lin = convertView.findViewById(R.id.tip_offs_img_item_lin);
            viewHolder.rel.getLayoutParams().width = rel_w;
            viewHolder.rel.getLayoutParams().height = rel_h;
            viewHolder.img_img.getLayoutParams().width = img_w;
            viewHolder.img_img.getLayoutParams().height = img_h;
            viewHolder.lin.getLayoutParams().width = img_w;
            viewHolder.lin.getLayoutParams().height = img_h;
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.close_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imgOnClickListener != null)
                    imgOnClickListener.del(position);
            }
        });
        viewHolder.lincarema.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imgOnClickListener != null)
                    imgOnClickListener.add();
            }
        });
        if (list.get(position).getType() == 0) {
            viewHolder.count_tv.setText(getCount() - 1 + "/4");
            viewHolder.lincarema.setVisibility(View.VISIBLE);
            viewHolder.count_tv.setVisibility(View.VISIBLE);
            viewHolder.close_img.setVisibility(View.INVISIBLE);
            viewHolder.img_img.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.count_tv.setText("");
            viewHolder.count_tv.setVisibility(View.INVISIBLE);
            viewHolder.lincarema.setVisibility(View.INVISIBLE);
            viewHolder.close_img.setVisibility(View.VISIBLE);
            viewHolder.img_img.setVisibility(View.VISIBLE);
            Glide.with(mContext)
                    .load(list.get(position).getPath())
                    .into(viewHolder.img_img);
        }
        return convertView;
    }

    public class ViewHolder {
        RelativeLayout rel;
        LinearLayout lin;
        TextView count_tv;
        ImageView close_img;
        ImageView img_img;
        LinearLayout lincarema;
    }


    public interface ImgOnClickListener {
        void add();//添加图片

        void del(int index); //删除图片
    }
}
